package com.mjk.manager.workflow.parser.sql.polling.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.mjk.common.tools.springextend.BeeSpringBean;
import com.mjk.manager.workflow.parser.constant.ReadType;
import com.mjk.manager.workflow.parser.sql.parse.ILiteParse;
import com.mjk.manager.workflow.parser.sql.polling.AbstractSqlReadPollTask;
import com.mjk.manager.workflow.parser.sql.read.SqlRead;
import com.mjk.manager.workflow.parser.sql.parse.impl.MagicParse;
import com.mjk.manager.workflow.parser.sql.parse.impl.WorkFlowParse;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.flow.FlowBus;

import org.springframework.util.CollectionUtils;

/**
 * chain 读取任务
 *
 * @author tangkc
 * @author houxinyu
 * @since 2.11.1
 */
public class ChainReadPollTask extends AbstractSqlReadPollTask {

    public ChainReadPollTask(SqlRead read) {
        super(read);
    }

    @Override
    public void doSave(Map<String, String> saveElementMap) {
    	//自定义业务流解析
        Map<String,ILiteParse> parses = BeeSpringBean.getBeansOfType(ILiteParse.class);
        if(!CollectionUtils.isEmpty(parses)){
            for(Map.Entry<String,ILiteParse> entity : parses.entrySet()){
                entity.getValue().parse(saveElementMap);
            }
        }

        for (Map.Entry<String, String> entry : saveElementMap.entrySet()) {
            String chainName = entry.getKey();
            String newData = entry.getValue();
            LiteFlowChainELBuilder.createChain().setChainId(chainName).setEL(newData).build();
        }
    }

    @Override
    public void doDelete(List<String> deleteElementId) {
        for (String id : deleteElementId) {
            FlowBus.removeChain(id);
        }
    }

    @Override
    public ReadType type() {
        return ReadType.CHAIN;
    }

}
